//logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    array: [
      '今天',
      '本周',
      '本月',
      '三月内',
      '半年内',
      '今年'
    ],
    index: '0',
    logs: [],
    totalTime: 0,
    longestTimeSchedule: {}
  },
  onLoad: function () {
    var index = this.data.index
    this.setLogs(index)
  },
  // 设定logs
  setLogs: function (index) {
    var logs = util.getLogs(index)
    var totalTime = this.setReport(logs)
    this.setData({
      index: index,
      logs: logs,
      totalTime: totalTime
    })
  },
  // 计算报告相关数据
  setReport: function (logs) {
    var totalTime = 0
    var longestTimeSchedule = {
      schedule: '',
      duration: 0
    }
    logs.forEach(function(item,index) {
      if(item.duration > longestTimeSchedule.duration) {
        longestTimeSchedule = {
          schedule: item.schedule,
          duration: item.duration,
          percentage: item.percentage
        }
      }
      totalTime += item.duration
    })
    this.setData({
      totalTime: totalTime,
      longestTimeSchedule: longestTimeSchedule
    })
  },
  dateChange: function (e) {
    var index = e.detail.value
    this.setLogs(index)
  }
})
